package com.fs.groupSeven.controller;

import com.fs.groupSeven.pojo.*;
import com.fs.groupSeven.pojo.Class;
import com.fs.groupSeven.service.StudentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import javax.servlet.annotation.WebServlet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:用于转发页面
 * @author: 周鑫
 * @createDate: 2020/9/28/028
 * @version: 1.0
 */
@Controller
@RequestMapping("/page")
public class PageTranspondController {

    @Autowired
    private StudentService studentService;

    //跳转到学生信息编辑页面
    @GetMapping("/studentInformationPage")
    public String getListView(ModelMap modelMap,WebRequest webRequest){
        //返回页面
        return "zhx/editStudentInformationPage";
    }


    //跳转到学生成绩页面
    @GetMapping("/studentGradePage")
    public String getGradeView(ModelMap modelMap,WebRequest webRequest){
        String collegeId = webRequest.getParameter("collegeId").trim();
        String classId = webRequest.getParameter("classId").trim();
        modelMap.addAttribute("collegeId",collegeId);
        modelMap.addAttribute("classId",classId);
        //返回页面
        return "zhx/editGradePage";
    }

    //查询班级
    @GetMapping("/showClassesByIndex")
    @ResponseBody
    public Map<String,Object> showClassesByIndex(){
        List<Class> classList = studentService.showClassesByIndex();
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取班级数据成功");
        result.put("classesIndex",classList);
        return result;
    }

    //根据学院id查询班级
    @GetMapping("/selSubjectsByCollegeId")
    @ResponseBody
    public Map<String,Object> selSubjectsByCollegeId(Integer collegeId){
        List<Subject> subjectList = studentService.querySubjectList(collegeId);
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取班级数据成功");
        result.put("subjectList",subjectList);
        return result;
    }

    //查询学院名称
    @GetMapping("/selCollegeNameList")
    @ResponseBody
    public Map<String,Object> selCollegeNameList(){
        List<College> collegeNameList = studentService.selCollegeNameList();
        System.out.println(collegeNameList);
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取班级数据成功");
        result.put("collegeNameList",collegeNameList);
        return result;
    }


    //根据学院id查询班级
    @GetMapping("/selClassList")
    @ResponseBody
    public Map<String,Object> selClassList(Integer collegeId){
        List<Class> classList = studentService.queryClassList(collegeId);
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取班级数据成功");
        result.put("classList",classList);
        return result;
    }


    //根据班级分页加载学生信息
    @RequestMapping("/selStudentsByClassId")
    @ResponseBody
    public Map<String,Object> selStudentsByClassId(Integer currentPage,Integer pageSize,Integer classId){

        PageInfo<Student> pageInfo = studentService.selStudentsByClassId(currentPage,pageSize,classId);

        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取用户数据成功");
        result.put("studentList",pageInfo.getList());//返回查询出来的分页数据
        result.put("totalPage",pageInfo.getPages());//返回当前总共有多少页
        return result;
    }


    //分页加载学生信息
    @RequestMapping("/loadListByPage")
    @ResponseBody
    public Map<String,Object> getListByPage(Integer currentPage,Integer pageSize){

        PageInfo<Student> pageInfo = studentService.getStudentsByPage(currentPage,pageSize);

        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取用户数据成功");
        result.put("studentList",pageInfo.getList());//返回查询出来的分页数据
        result.put("totalPage",pageInfo.getPages());//返回当前总共有多少页
        return result;
    }


    //添加，编辑学生信息
    @RequestMapping("/edit")
    @ResponseBody
    public Map<String,Object> edit(Student student){
        Map<String,Object> result = new HashMap<>();
        boolean modified;
        if(student.getStudentId() > 0){
            //编辑用户
            modified = studentService.modifyOne(student);
        }else {
            //添加用户
            modified = studentService.addOne(student);
        }
        if(!modified){
            result.put("code",-1);
            result.put("message","操作失败");
        }else {
            result.put("code",0);
            result.put("message","编辑成功,请稍后");
        }
        return result;
    }

    //编辑学生信息
    @RequestMapping("/getStudentDetailById")
    @ResponseBody
    public Map<String,Object> getOne(Integer studentId){
        Student student = studentService.getOneByStudentId(studentId);
        Map<String,Object> result = new HashMap<>();
        if(student==null){
            result.put("code",-1);
            result.put("message","获取学生信息失败");
        }else {
            result.put("code",0);
            result.put("message","获取学生信息成功");
            result.put("student",student);

        }
        return result;
    }

    //删除学生
    @RequestMapping("/delStudent")
    @ResponseBody
    public Map<String,Object> delStudentByStudentId(Integer studentId){
        int i = studentService.delStudentByStudentId(studentId);
        Map<String,Object> result = new HashMap<>();
        if(i == 0){
            result.put("code",-1);
            result.put("message","删除学生信息失败");
        }else {
            result.put("code",0);
            result.put("message","删除学生信息成功");
        }
        return result;
    }

    //模糊查询用户
    @RequestMapping("/searchStudents")
    @ResponseBody
    public Map<String,Object> searchUsers(Integer currentPage,Integer pageSize,String searchValue,Integer searchBox){
        List<Student> students;
        if (searchBox == 1){//查询当前页
             students = studentService.searchStudentsByPage(currentPage, pageSize, searchValue);
        }else {
            students = studentService.searchStudentsByAll(searchValue);
        }
        Map<String,Object> result = new HashMap<>();
        if (students.size() != 0){
            result.put("code",0);
            result.put("message","获取学生信息成功");
            result.put("students",students);
        }else {
            result.put("code",-1);
            result.put("message","你所查询的记录为不存在，请重新查询");
        }
            return result;
    }



    //添加，编辑学生成绩
    @RequestMapping("/editGrade")
    @ResponseBody
    public Map<String,Object> editGrade(Integer gradeId,Integer stuNum,String name ,String subjectName,Integer usualscore,Integer examscore){
        Map<String,Object> result = new HashMap<>();
        //定义最终总成绩
        Integer finalscore = usualscore + examscore;

        if (gradeId != 0){//编辑成绩
            int editGrade = studentService.editGradeByGradeId(gradeId,usualscore,examscore,finalscore);
            if (editGrade > 0){
                result.put("code",0);
                result.put("message","修改数据成功");
            }
        }else {//添加成绩
            //根据学号查询学生id，姓名及班级id
            List<Student> info = studentService.queryStudentSomeInformationByStuNum(stuNum);
            //判断输入的学号是否存在
            if (info.size() == 0){//学号不存在
                result.put("code",-1);
                result.put("message","对不起，你所录入的学号不存在,请重新输入");
            }else {//学号存在
                //根据学号查询出来的结果与输入的姓名进行匹配
                if (!info.get(0).getName().equals(name)){//不匹配
                    result.put("code",-2);
                    result.put("message","对不起，你所输入的学号与姓名不匹配，请重新录入");
                }else {//匹配
                    //将info中的学生id进行另外命名
                    Integer studentId = info.get(0).getStudentId();
                    //根据学生id和科目名称判断成绩是否已经存在
                    boolean gradeInfo = studentService.queryScoreIsExistByStudentIdAndSubjectName(subjectName,studentId);
                    if (gradeInfo){//表示输入的科目已经存在
                        result.put("code" ,-3);
                        result.put("message","对不起," + name + "的" + subjectName + "的成绩已经存在");
                    }else {//表示不存在,进行添加成绩
                        int addGrade = studentService.addGrade(studentId,subjectName,usualscore,examscore,finalscore);
                        if (addGrade > 0){
                            result.put("code",0);
                            result.put("message","录入成功，请稍后");
                        }else {
                            result.put("code",-4);
                            result.put("message","录入失败");
                        }
                    }
                }
            }
        }
      return result;
    }

    //分页加载学生成绩信息
    @RequestMapping("/loadGradeTable")
    @ResponseBody
    public Map<String,Object> getGradeListByPage(Integer currentPage,Integer pageSize,Integer classId,String subjectName){
        PageInfo<Grade> pageInfo = studentService.getGradeTable(currentPage,pageSize,classId,subjectName);
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取用户数据成功");
        result.put("gradeList",pageInfo.getList());//返回查询出来的分页数据
        result.put("totalPage",pageInfo.getPages());//返回当前总共有多少页
        return result;
    }

    //编辑学生成绩
    @RequestMapping("/getStudentGradeByGradeId")
    @ResponseBody
    public Map<String,Object> getOneGradeByGradeId(Integer gradeId){
        Student grade = studentService.getOneGradeByGradeId(gradeId);
        Map<String,Object> result = new HashMap<>();
        if(grade==null){
            result.put("code",-1);
            result.put("message","获取学生成绩失败");
        }else {
            result.put("code",0);
            result.put("message","获取学生成绩成功");
            result.put("grade",grade);

        }
        return result;
    }


    //模糊查询学生成绩
    @RequestMapping("/searchStudentGrade")
    @ResponseBody
    public Map<String,Object> searchStudentGrade(Integer currentPage,Integer pageSize,String searchValue){
        List<Student> studentGrade = studentService.searchStudentGrade(searchValue);
        Map<String,Object> result = new HashMap<>();
        if (studentGrade.size() != 0){
            result.put("code",0);
            result.put("message","获取学生信息成功");
            result.put("studentGrade",studentGrade);
        }else {
            result.put("code",-1);
            result.put("message","你所查询的记录为不存在，请重新查询");
        }
        return result;
    }


}
